# 在设置环境变量之前检查当前工作目录和文件
import os
import sys
from pathlib import Path
print(f"当前工作目录: {os.getcwd()}")
print(f"当前文件: {__file__}")
print(f"sys.path:{sys.path}")

sys.path.insert(0, str(os.getcwd()))
from llm_sandbox.exceptions import SecurityViolationError

from app.core.langgraph.tools.execute_code_tool import execute_code_tool



# 将项目根目录添加到sys.path的开头
# sys.path.insert(0, str(project_root))


import docker
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI

from llm_sandbox import SandboxSession, SandboxBackend

from app.core.config import settings
from app.core.logging import logger




if __name__ == "__main__":
    llm = ChatOpenAI(
            base_url=settings.LLM_BASE_URL,
            model=settings.LLM_MODEL, #deepseek-chat
            temperature=0, #0.7
            api_key=settings.LLM_API_KEY, #sk-cb3aacb93d554d53a0890c5b00d6faa9
        )
    tools = [execute_code_tool]
    all_tools = {"execute_code_tool": execute_code_tool}
    llm_with_tools=llm.bind_tools(tools)
    res = llm_with_tools.invoke("计算一下pi的精确数值，精确到小数点后20位")
    print(res)
    if hasattr(res, 'tool_calls') or res.tool_calls:
        for tool_call in res.tool_calls:
            logger.info(f"toolname={tool_call.get('name')}")
            logger.info(f"toolargs={tool_call.get('args')}")
            logger.info(f"toolid={tool_call.get('id')}")
            selected_tool = all_tools[tool_call["name"].lower()]
            tool_message = selected_tool.invoke(tool_call)
            print(tool_message)

